package me.zk.practice1.hw;

import java.util.*;

/**
 * @author zhangkai
 * @date 2023/8/7 13:22
 */
public class HWBJ74 {
    static Map<Integer,Node> map = new HashMap<>();
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt();

        Integer n = scanner.nextInt();
        scanner.nextLine();

        for (int i = 0; i < m; i++) {
            String s = scanner.nextLine();
            String[] strs = s.split(" ");
            Integer id = Integer.parseInt(strs[0]);

            Integer size = Integer.parseInt(strs[1]);

            strs[2] = strs[2].replace("(","").replace(")","").trim();
            List<Integer> list = new ArrayList<>();

            if (strs[2].length() > 0) {
                String[] childs = strs[2].split(",");
                for (String c : childs) {
                    list.add(Integer.parseInt(c));
                }
            }


            map.put(id,new Node(id,size,list));
        }
        int sum = handle(n);

        System.out.println(sum);

    }


    public static int handle(Integer n) {
        int sum = 0;
        if (map.containsKey(n)) {
            Node node = map.get(n);
            sum += node.size;
            for (Integer s: node.child) {
                sum+=handle(s);
            }
            return sum;
        }
        return sum;

    }
    static class Node {
        Integer id;

        Integer size;

        List<Integer> child;

        public Node(Integer id,Integer size, List<Integer> list) {
            this.id = id;
            this.child = list;
            this.size = size;
        }
    }
}
